import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import axios from 'axios'

export async function getFilesList() {
  return service.get('/files')
}

export async function createDoc(data) {
  return service.post('/files', data)
}

export async function renameDoc(id, name) {
  return service.put(`/files/rename/${id}/${name}`).then(() => {
    ElMessage.success('文档重命名成功')
  })
}

export async function deleteDoc(id) {
  return service.delete(`/files/${id}`)
}

export async function moveTag(id, tagId) {
  return service.put(`/files/move/${id}/${tagId}`)
}

export async function updateDoc(data) {
  return service.put('/files', data)
}

export async function uploadDoc(data) {
  return service
    .post('/files', data)
    .then((res) => {
      return res.data
    })
    .catch((err) => {
      if (err.response.status === 413) {
        ElMessage.error('文件过大，请上传小于10MB的文件')
      } else {
        ElMessage.error('上传失败，请重试')
      }
    })
}

export async function downloadDoc(id) {
  return await service.get(`/files/download/${id}`, {
    responseType: 'blob',
  })
}
